package com.hz.userservice.service.impl;

import com.alibaba.fastjson.JSON;
import com.hz.userservice.helper.UserHelper;
import com.hz.userservice.mapper.ClassMapper;
import com.hz.userservice.mapper.GradeMapper;
import com.hz.userservice.mapper.UserMapper;
import com.hz.userservice.model.entity.ClassEntity;
import com.hz.userservice.model.entity.GradeEntity;
import com.hz.userservice.model.entity.UserEntity;
import com.hz.userservice.model.enums.UserTypeEnum;
import com.hz.userservice.service.UserManagerService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class UserManagerServiceImpl implements UserManagerService {
    @Resource
    GradeMapper gradeMapper;
    @Resource
    ClassMapper classMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    RedisTemplate redisTemplate;
    @Override
    public int addGrade(String gradeName, String gradeYear) {
        if (gradeMapper.findByGradeName(gradeName)!=null){
            return 0;
        }
        GradeEntity gradeEntity=new GradeEntity();
        gradeEntity.setGrade_name(gradeName);
        gradeEntity.setGrade_year(gradeYear);

        return gradeMapper.addGrade(gradeEntity);
    }

    @Override
    public int addClass(String className, int gradeId) {
        GradeEntity gradeEntity=gradeMapper.findByGradeId(gradeId);
        if (gradeEntity==null){
            return 0;
        }
        if (classMapper.findByClassName(className)!=null){
            return 0;
        }

        ClassEntity classEntity=new ClassEntity();
        classEntity.setClass_name(className);
        classEntity.setGrade_id(gradeId);
        classEntity.setGrade_name(gradeEntity.getGrade_name());
        classEntity.setGrade_year(gradeEntity.getGrade_year());

        return classMapper.addClass(classEntity);
    }

    @Override
    public int addManager(String username, String realname, String password) {
        if (userMapper.findByUsernameAndUsertype(username, UserTypeEnum.MANAGER.getType())!=null){
            return 0;
        }
        UserEntity userEntity=new UserEntity();
        userEntity.setUsername(username);
        userEntity.setRealname(realname);
        userEntity.setUser_type(UserTypeEnum.MANAGER.getType());

        String pwd_salt= UUID.randomUUID().toString().replace("-","");
        String pwd= UserHelper.createNewPwd(password,pwd_salt);
        userEntity.setPwd(pwd);
        userEntity.setPwd_salt(pwd_salt);
        userEntity.setPwd_valid_date(System.currentTimeMillis()+7*24*3600*1000);

        return userMapper.addUser(userEntity);
    }

    @Override
    public int addTeacher(String username, String realname, String password, String schoolId) {
        if (userMapper.findByUsernameAndUsertype(username, UserTypeEnum.STUDENT.getType())!=null){
            return 0;
        }
        UserEntity userEntity=new UserEntity();
        userEntity.setUsername(username);
        userEntity.setRealname(realname);
        userEntity.setStudent_id(schoolId);
        userEntity.setUser_type(UserTypeEnum.TEACHER.getType());

        String pwd_salt= UUID.randomUUID().toString().replace("-","");
        String pwd= UserHelper.createNewPwd(password,pwd_salt);

        userEntity.setPwd(pwd);
        userEntity.setPwd_salt(pwd_salt);
        userEntity.setPwd_valid_date(System.currentTimeMillis()+7*24*3600*1000);

        return userMapper.addUser(userEntity);
    }

    @Override
    public int addStudent(String username, String realname, String password, String studentId, Integer gradeId, Integer classId) {
        if (userMapper.findByUsernameAndUsertype(username, UserTypeEnum.STUDENT.getType())!=null){
            return 0;
        }
        UserEntity userEntity=new UserEntity();
        userEntity.setUsername(username);
        userEntity.setRealname(realname);
        userEntity.setStudent_id(studentId);
        userEntity.setUser_type(UserTypeEnum.STUDENT.getType());
        userEntity.setGrade_id(gradeId);
        userEntity.setClass_id(classId);

        String pwd_salt= UUID.randomUUID().toString().replace("-","");
        String pwd= UserHelper.createNewPwd(password,pwd_salt);

        userEntity.setPwd(pwd);
        userEntity.setPwd_salt(pwd_salt);
        userEntity.setPwd_valid_date(System.currentTimeMillis()+7*24*3600*1000);

        return userMapper.addUser(userEntity);
    }

    @Override
    public int editManager(Integer id, String username, String realname, String password) {
        UserEntity userEntity=userMapper.findById(id);
        if (userEntity!=null){
            return 0;
        }

        return userMapper.addUser(userEntity);
    }

    @Override
    public int editTeacher(Integer id, String username, String realname, String password, String schoolId) {
        return 0;
    }

    @Override
    public int editStudent(Integer id, String username, String realname, String password, String studentId, Integer gradeId, Integer classId) {
        return 0;
    }

    @Override
    public String login(String username, String password, Integer userType) {
        UserEntity userEntity=userMapper.findByUsernameAndUsertype(username,userType);
        if (userEntity==null){
            return null;
        }
        String enterPwd=UserHelper.createNewPwd(password,userEntity.getPwd_salt());
        if (!enterPwd.equals(userEntity.getPwd())){
            return null;
        }

        redisTemplate.opsForValue().set(username, JSON.toJSONString(userEntity),30, TimeUnit.MINUTES);

        return JSON.toJSONString(userEntity);
    }

    @Override
    public int logout(String username) {
        redisTemplate.delete(username);
        return 1;
    }

    @Override
    public int changePwd(Integer id, String originPwd, String newPwd) {

        UserEntity userEntity=userMapper.findById(id);
        String pwd=userEntity.getPwd();
        String pwd_salt=userEntity.getPwd_salt();
        if (!pwd.equals(UserHelper.createNewPwd(originPwd,pwd_salt))){
            return 0;
        }
        String newPassword=UserHelper.createNewPwd(newPwd,pwd_salt);
        if (pwd.equals(newPassword)){
            return 0;
        }  
        if (userMapper.updatePwd(id,newPassword,System.currentTimeMillis()+7*24*3600*1000)>0){
            redisTemplate.delete(userEntity.getUsername());
            return 1;
        }
        else {
            return 0;
        }
    }

}
